#include "loginpage.h"
#include "ui_loginpage.h"
#include "QDebug"
LoginPage::LoginPage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::LoginPage)
{
    ui->setupUi(this);
    accountLabel = new QLabel(tr("accout"),this);
    accountLabel->setMinimumWidth(40);
    accountLabel->setMaximumWidth(40);
    accountLine = new QLineEdit(this);
    accountLine->setPlaceholderText("input account");
    QHBoxLayout *accoutBox = new QHBoxLayout();
    accoutBox->addWidget(accountLabel);
    accoutBox->addWidget(accountLine);

    passwordLabel = new QLabel(tr("password"),this);
    passwordLine = new QLineEdit(this);
    passwordLabel->setMaximumWidth(40);
    passwordLabel->setMinimumWidth(40);
    QHBoxLayout *passwordBox = new QHBoxLayout();
    passwordLine->setPlaceholderText("input password");
    passwordBox->addWidget(passwordLabel);
    passwordBox->addWidget(passwordLine);

//    QVBoxLayout* temp1 = new QVBoxLayout();
//    temp1->addWidget(accountLabel);
//    temp1->addWidget(passworddLabel);

//    QVBoxLayout* temp2 = new QVBoxLayout();
//    temp2->addWidget(accountLine);
//    temp1->addWidget(passworddLine);

    QVBoxLayout *leftVBox = new QVBoxLayout();
//        leftVBox->addLayout(temp1);
//        leftVBox->addLayout(temp2);
    leftVBox->addLayout(accoutBox);
    leftVBox->addLayout(passwordBox);

    registerButtton = new QPushButton(tr("register"),this);
    loginButton = new QPushButton(tr("login"),this);
    clearButton = new QPushButton(tr("clear"),this);
    exitButton = new QPushButton(tr("exit"),this);
    QVBoxLayout *buttonBox = new QVBoxLayout();
    buttonBox->addWidget(registerButtton);
    buttonBox->addWidget(loginButton);
    buttonBox->addWidget(clearButton);
    buttonBox->addWidget(exitButton);

    QHBoxLayout* mainBox = new QHBoxLayout();
    mainBox->addLayout(leftVBox);
    mainBox->addLayout(buttonBox);
    this->setLayout(mainBox);

    connect(registerButtton,&QPushButton::clicked,
            this,&LoginPage::toRegister);
    connect(loginButton,&QPushButton::clicked,
            this,&LoginPage::checkPwd);
    connect(clearButton,&QPushButton::clicked,
            this,&LoginPage::clearAll);
    connect(exitButton,&QPushButton::clicked,
            this,&QCoreApplication::quit);

    userFile = new FileIO();
}

LoginPage::~LoginPage()
{
    delete ui;
}

void LoginPage::toRegister()
{
    emit toPage("register");
    emit destory();
}

void LoginPage::checkPwd()
{
    if(accountLine->text().isEmpty())
    {
        QMessageBox::warning(this,"账号为空","请输入账号");
        return;
    }
    if(passwordLine->text().isEmpty())
    {
        QMessageBox::warning(this,"密码为空","请输入密码");
          return;
    }
    QString accout = accountLine->text();
    QString password = passwordLine->text();
    if(userFile->checkPassword(User(accout,password)))
    {
        emit toPage("home");
        emit destory();
    }
    else
    {
        QMessageBox::critical(this,"密码错误","请输入密码");
                clearPwd();
    }
}

void LoginPage::clearAll()
{
    clearPwd();
    accountLine->setText("");
}

void LoginPage::clearPwd()
{
    passwordLine->setText("");

}
